home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / python-xdg / xdg / Locale.py < prev    next >
Encoding:
Python Source  |  2005-05-26  |  1.8 KB  |  80 lines

  1. """
  2. Helper Module for Locale settings
  3.  
  4. This module is based on a ROX module (LGPL):
  5.  
  6. http://cvs.sourceforge.net/viewcvs.py/rox/ROX-Lib2/python/rox/i18n.py?rev=1.3&view=log
  7. """
  8.  
  9. import os
  10. from locale import normalize
  11.  
  12. regex = "(\[([a-zA-Z]+)(_[a-zA-Z]+)?(\.[a-zA-Z\-0-9]+)?(@[a-zA-Z]+)?\])?"
  13.  
  14. def _expand_lang(locale):
  15.     locale = normalize(locale)
  16.     COMPONENT_CODESET   = 1 << 0
  17.     COMPONENT_MODIFIER  = 1 << 1
  18.     COMPONENT_TERRITORY = 1 << 2
  19.     # split up the locale into its base components
  20.     mask = 0
  21.     pos = locale.find('@')
  22.     if pos >= 0:
  23.         modifier = locale[pos:]
  24.         locale = locale[:pos]
  25.         mask |= COMPONENT_MODIFIER
  26.     else:
  27.         modifier = ''
  28.     pos = locale.find('.')
  29.     codeset = ''
  30.     if pos >= 0:
  31.         locale = locale[:pos]
  32.     pos = locale.find('_')
  33.     if pos >= 0:
  34.         territory = locale[pos:]
  35.         locale = locale[:pos]
  36.         mask |= COMPONENT_TERRITORY
  37.     else:
  38.         territory = ''
  39.     language = locale
  40.     ret = []
  41.     for i in range(mask+1):
  42.         if not (i & ~mask):  # if all components for this combo exist ...
  43.             val = language
  44.             if i & COMPONENT_TERRITORY: val += territory
  45.             if i & COMPONENT_CODESET:   val += codeset
  46.             if i & COMPONENT_MODIFIER:  val += modifier
  47.             ret.append(val)
  48.     ret.reverse()
  49.     return ret
  50.  
  51. def expand_languages(languages=None):
  52.     # Get some reasonable defaults for arguments that were not supplied
  53.     if languages is None:
  54.         languages = []
  55.         for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
  56.             val = os.environ.get(envar)
  57.             if val:
  58.                 languages = val.split(':')
  59.                 break
  60.     #if 'C' not in languages:
  61.     #    languages.append('C')
  62.  
  63.     # now normalize and expand the languages
  64.     nelangs = []
  65.     for lang in languages:
  66.         for nelang in _expand_lang(lang):
  67.             if nelang not in nelangs:
  68.                 nelangs.append(nelang)
  69.     return nelangs
  70.  
  71. def update(language=None):
  72.     global langs
  73.     if language:
  74.         langs = expand_languages([language])
  75.     else:
  76.         langs = expand_languages()
  77.  
  78. langs = []
  79. update()
  80.